# mail.py
# %g4Z1u[AOPG#

import smtplib
import base64


class Mail(object):
    """docstring for Mail"""

    def __init__(self, smtp, usr, pwd, ssl=False):
        super(Mail, self).__init__()
        self.smtp_server = smtp
        self.usr = usr
        self.pwd = pwd
        self.ssl = ssl
        self.worker = None

    def set_mail_config(self):
        self.worker = smtplib.SMTP(self.smtp_server)
        self.worker.set_debuglevel(1)
        self.worker.docmd("EHLO server")
        self.worker.starttls()
        self.worker.docmd("AUTH LOGIN")
        self.worker.send(base64.encodestring(self.usr))
        self.worker.getreply()
        self.worker.send(base64.encodestring(self.pwd))
        self.worker.getreply()
        return self.worker

    def send_text(self, target, content="Hello~"):
        self.worker.docmd(f"MAIL FROM: <{self.usr}>")
        self.worker.docmd(f"RCPT TO: <{target}>")
        self.worker.docmd("DATA")
        self.worker.send(content.decode("utf8"))
        self.worker.send(" . ")
        self.worker.getreply()
        self.worker.quit()

if __name__ == '__main__':
    worker = Mail("mail.io.cacsite.com",
                  "tester@io.cacsite.com", r"%g4Z1u[AOPG#", True).set_mail_config()
    worker.send_text(worker.usr)
